'use strict';
/**
 * Created by FreezeSoul on 2015/12/8.
 */
define([
        'angular',
        'adConfig',
        'app/modules/player/define',
        'app/modules/player/services/player',
        'app/modules/player/controllers/index-selected',
        'app/modules/editor/directives/dis-right-click'
    ],
    function (angular, adConfig) {
        angular.module('ad.player')
            .controller('adPlayerController', ['$rootScope', '$scope', '$state', '$aside', '$timeout', '_', 'Notification', 'adPlayerService'
                , function ($rootScope, $scope, $state, $aside, $timeout, _, Notification, adPlayerService) {
                    $scope.isShowSidebar = false;
                    $scope.switchSidebar = function () {
                        $scope.isShowSidebar = !$scope.isShowSidebar;
                        return false;
                    };

                    $scope.layoutPlugin = undefined;
                    $scope.indexPageId = undefined;
                    $scope.currentPageId = undefined;

                    $scope.pivotPageItems = [];

                    $scope.UserPages = [];

                    $scope.openPageInfo = function () {
                        $aside.open({
                            size: 'sm',
                            backdrop: true,
                            placement: 'right',
                            templateUrl: 'app/modules/player/views/index-selected.tpl.html',
                            controller: 'adIndexSelectController',
                            resolve: {
                                layout: function () {
                                    return $scope.layoutPlugin;
                                }
                            }
                        }).result.then(function () {
                        }, function () {
                        });
                        return false;
                    };

                    $scope.savePageInfo = function () {
                        if (!$scope.layoutPlugin) return;
                        var pageOption = angular.toJson($scope.layoutPlugin.getOption());
                        adPlayerService.addOrUpdatePage($scope.currentPageId, pageOption).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                Notification.success('保存成功！');
                            }
                        });
                    };

                    $scope.restorePageInfo = function () {
                        adPlayerService.restoreDefaultPage($scope.currentPageId).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                if ($scope.pivotPageItems.length > 0) {
                                    var page = $scope.pivotPageItems[$scope.pivotPageItems.length - 1];
                                    $scope.layoutPlugin.navigate(-1, {});
                                    $scope.layoutPlugin.navigate(page.page_id, page.page_param);
                                } else {
                                    $scope.indexPageId = -1;
                                    $timeout(function () {
                                        $scope.indexPageId = obj.data.page_id;
                                    }, 100);
                                }
                                Notification.success('还原成功！');
                            }
                        });
                    };

                    $scope.loadUserPage = function (pageId) {
                        adPlayerService.loadUserPage(pageId).then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                $scope.pivotPageItems = [];
                                $scope.indexPageId = obj.data.page_id;
                                $scope.currentPageId = $scope.indexPageId;
                            }
                        });
                    };

                    $scope.loadUserPages = function () {
                        adPlayerService.loadUserPages().then(function (data) {
                            var obj = angular.fromJson(data);
                            if (obj.result == 'success') {
                                $scope.UserPages = obj.data;
                                if ($scope.UserPages.length > 0) {
                                    $scope.indexPageId = -1;
                                    $scope.indexPageName = $scope.UserPages[0].page_name;
                                    //$scope.loadUserPage($scope.UserPages[0].page_id);
                                    $scope.pivotPageItems = [];
                                    $scope.indexPageId = $scope.UserPages[0].page_id;
                                    $scope.currentPageId = $scope.indexPageId;
                                }
                            }
                        });
                    };

                    $scope.navigateIndexClick = function (pageId, pageName) {
                        $scope.indexPageId = -1;
                        $scope.indexPageName = pageName;
                        $scope.currentPageId = $scope.indexPageId;
                        $scope.loadUserPage(pageId);
                    };

                    $scope.navigatePageClick = function (page, index) {
                        if (index == $scope.pivotPageItems.length - 1) return;
                        $scope.currentPageId = page.page_id;
                        $scope.pivotPageItems = $scope.pivotPageItems.slice(0, index);
                        $scope.layoutPlugin.navigate(page.page_id, page.page_param);
                    };

                    $scope.loadUserPages();

                    var unbind = $rootScope.$on('ad.appWeb.http.error', function (event, arg) {
                        var url = arg.config.url.replace(adConfig.baseServiceUrl, '');
                        Notification.error('数据服务异常！' + '<br/>Status:' + arg.status + '<br/>URL:' + url);
                    });
                    $scope.$on('$destroy', unbind);
                }]);
    });